package com.education.welco.controller.teacherapp;

import com.education.common.config.BusinessException;
import com.education.common.pojo.AjaxResult;
import com.education.common.utils.Token;
import com.education.welco.feign.userFeign;
import com.education.welco.pojo.JrOpinion;
import com.education.welco.pojo.TTeacher;
import com.education.welco.pojo.User;
import com.education.welco.pojo.vo.AppTeaInfoVO;
import com.education.welco.service.IJrOpinionService;
import com.education.welco.service.ITTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: liftsail
 * @Date: 2023/1/30 15:23
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/apptea/profile")
public class AppTeaProfileController {

    @Autowired
    private userFeign userFeign;
    @Autowired
    private ITTeacherService teacherService;
    @Autowired
    private IJrOpinionService jrOpinionService;

    /**
     * 名字、头像、学校、班级数、课程数、版本号
     *
     * @param teaId
     * @return
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam(value = "teaId", required = false) Integer teaId) {
        //用户信息
        AppTeaInfoVO teacherInfo = teacherService.selectTeacherInfoByUserId(teaId);
        //版本信息
        return AjaxResult.success(teacherInfo);
    }

    /**
     * 教师信息更新
     * userId
     * teacherImage
     * teacherName
     */
    @PostMapping("/update")
    public AjaxResult update(TTeacher teacher) throws BusinessException {
        teacherService.updateTeacherByUserId(teacher);
        return AjaxResult.success("修改成功");
    }

    /**
     * 检测原密码是否输入正确
     *
     * @param teaUserId
     * @param oldPassWord
     * @return
     */
    @GetMapping("/oldPassWordCheck")
    public AjaxResult oldPassWordCheck(Integer teaUserId, String oldPassWord) {
        //查找原密码
        //User user = userService.selectSysUserById(userId);
        User user = userFeign.selectUserById(teaUserId);
        //数据库密码 和 传过来的原密码进行比较
        String oldMd5String = Token.getMD5String(oldPassWord);
        //密码匹配 设置新密码
        if (oldMd5String.equals(user.getSysPassword())) {
            return AjaxResult.success("原密码输入正确");
        }
        return AjaxResult.error("原密码输入错误");
    }

    /**
     * 新密码经过比对后进行修改
     *
     * @param teaUserId
     * @return
     * @throws Exception
     */
    @PostMapping("/passWordUpdate")
    public AjaxResult forGetPassWordUpdate(Long teaUserId, String oldPassword, String newPassword) {
        if (oldPassword.equals(newPassword)) {
            return AjaxResult.error("新旧密码一致");
        }
        User user = new User();
        user.setId(teaUserId);
        user.setSysPassword(Token.getMD5String(newPassword));
        userFeign.updatePassword(user);
        return AjaxResult.success("密码修改成功");
    }

    /**
     * 新增保存意见反馈
     * source 1.安卓2.苹果
     * yjUserid 用户id
     * yjIco 反馈图片
     * yjText 反馈内容
     * yjPhone 联系手机号
     */
    @PostMapping("/addOpinion")
    public AjaxResult addSave(JrOpinion jrOpinion) {
        jrOpinionService.insertJrOpinion(jrOpinion);
        return AjaxResult.success("意见反馈成功");
    }
}

